import { setIsConversation, setWebRtcData } from '@/redux/actions';

export const initSocket = (dispatch: any) => {
  console.log('websocket连接成功');
  WebSocket.socket.on('test', (data: string) => {
    console.log('11111111111', JSON.parse(data));
  });

  // 接收别人发起的通话
  WebSocket.socket.on('conversation', (data: any) => {
    console.log('通话', data);
    dispatch(setIsConversation(true)); // 设置通话界面显示
    dispatch(setWebRtcData({ toUser: data.fromUser }));
  });

  WebSocket.socket.on('disconnect', () => {
    console.log('websocket连接断开');
    WebSocket.socket.close();
  });
};

export module WebSocket {
  export var socket: any = {
    on: () => {
      console.log('已经断开');
    },
    emit: () => {
      console.log('已经断开');
    },
  };
}
